const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    isShow:true,
    isNone:false,
    idCardlist:'',
    keyword:''
  },
 
onLoad:function (){
  this.getidCard();
},
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },

  //获取名片列表
  getidCard: function () {
    var vm = this;
    app.core('get', 'get_user_card', {
    }, (res) => {
      vm.setData({
        idCardlist:res.data
      })

    },
      (err) => {
        console.log(err)
      }
    )
  },

  //删除名片
  removeItem(e){
    var vm = this;
    wx.showModal({
      title: '系统提示',
      content: '确定要删除当前名片吗',
      success(res) {
        if (res.confirm) {
          //删除
          var id = e.currentTarget.dataset.id
          var URL = "del_user_card/id/"+id;
          app.core('get', URL, {
          }, (res) => {
            setTimeout(function(){
              vm.onLoad();
            },500);
           
          },
            (err) => {
              console.log(err)
            }
          )

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //查看名片详情
  descItem(e){
    console.log(e);
    var Url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: Url,
    })
  },

  //搜索名片
  searchlist:function (e) {
    var keyword = (e.detail.value).replace(/\s+/g, '');
    if (keyword == ''){
      this.setData({
        isShow: true,
        isNone: false,
      });
    }else{
      this.setData({
        keyword: keyword
      })
    }
  },

  searchdesc:function(){
    var vm = this;
    var keys = (vm.data.keyword).replace(/\s+/g, '');
    if (keys ==''){
      wx.showToast({
        title: '请输入关键字',
        duration: 2000,
        icon: 'none'
      })
    }else{
      app.core('get', 'get_user_card', {
        keywords: keys
      }, (res) => {
        if (res.data.length == 0){
          vm.setData({
            isShow: false,
            isNone: true,
          });
        }else{
          vm.setData({
            isShow: true,
            isNone: false,
          });

          vm.setData({
            idCardlist: res.data
          })
        }
      },
        (err) => {
          console.log(err)
        }
      )
    }
  }


})